package com.bykj.property.controller;

import com.bykj.property.service.ChargeTypeService;
import com.bykj.property.model.ChargeType;
import com.bykj.property.config.common.exception.CustomException;
import com.bykj.property.controller.vo.response.ErrorCode;
import com.bykj.property.controller.vo.response.RespResult;
import com.bykj.property.controller.vo.request.ChargeTypePageRequest;
import com.bykj.property.controller.vo.export.ChargeTypeExport;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.bykj.property.util.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletRequest;
import org.springframework.validation.BindingResult;

import javax.validation.ValidationException;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import com.alibaba.fastjson.JSON;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import javax.validation.constraints.NotEmpty;

    import org.springframework.web.bind.annotation.RestController;

/**
* <p>
    * 收费类型表 前端控制器
    * </p>
*
* @author liangyiming
* @since 2019-06-20
*/

@Slf4j
    @Api(tags = {"收费类型表"})
    @RestController
   @RequestMapping("/admin/ChargeType")
        public class ChargeTypeController {
    @Autowired
    ChargeTypeService targetService;

    @ApiOperation(value="添加")
    @RequestMapping(value = "/add",method = RequestMethod.POST,produces = "application/json;charset=UTF-8" )
    public RespResult add(ChargeType model, BindingResult bindingResult) {
    return RespResult.ok(targetService.save(model));
    }

    @ApiOperation(value="更新")
    @RequestMapping(value = "/update",method = RequestMethod.POST,produces = "application/json;charset=UTF-8" )
    public RespResult update(ChargeType model) {
    return RespResult.ok(targetService.updateById(model));
    }

    @ApiOperation(value="删除接口")
    @RequestMapping(value = "/delete",method = RequestMethod.POST,produces = "application/json;charset=UTF-8" )
    public RespResult delete(@NotEmpty String id) {
    return RespResult.ok(targetService.removeById(id));
    }

    @ApiOperation(value="查询列表接口")
    @RequestMapping(value = "/query",method = RequestMethod.POST,produces = "application/json;charset=UTF-8" )
    public RespResult query(ChargeTypePageRequest pageRequest, BindingResult bindingResult) {
    if(pageRequest.getSize()==-1){
    return RespResult.ok(targetService.list());
    }
    return RespResult.ok(targetService.queryPage(pageRequest));
    }

    @ApiOperation(value="查询详情接口")
    @ApiImplicitParams({
    @ApiImplicitParam(name="id",value="id",dataType="String", paramType = "query")
    })
    @RequestMapping(value = "/get",method = RequestMethod.POST,produces = "application/json;charset=UTF-8" )
    public RespResult get(@NotEmpty String id) {
    return RespResult.ok(targetService.getById(id));
    }
    // 导出操作
    @ApiOperation(value="导出excel表格")
    @RequestMapping("/exportExcel")
    public void export(ChargeTypePageRequest pageRequest,HttpServletResponse response) {
    List<ChargeType> list =null;
    if(pageRequest.getSize()==-1){
    list=targetService.list();
    }else{
    list=targetService.page(pageRequest).getRecords();
    }

    // 模拟从数据库获取需要导出的数据
    List<ChargeTypeExport> listExport = JSON.parseArray(JSON.toJSONString(list),ChargeTypeExport.class);
    // 导出操作
    ExcelUtils.exportExcel(listExport, "收费类型表", "ChargeType", ChargeTypeExport.class, "ChargeType2019-06-20.xls", response);

    }
    @ApiOperation(value="导入excel表格")
    @PostMapping("/importExcel")
    public String importExcel2(@RequestParam("file") MultipartFile file) {
    ImportParams importParams = new ImportParams();
    // 数据处理
    importParams.setHeadRows(1);
    importParams.setTitleRows(1);
    // 需要验证
    importParams.setNeedVerfiy(false);

    try {
    ExcelImportResult<ChargeTypeExport> result = ExcelImportUtil.importExcelMore(file.getInputStream(), ChargeTypeExport.class,
        importParams);
        List<ChargeTypeExport> list = result.getList();
            for (ChargeTypeExport model : list) {

            log.info("从Excel导入数据到数据库的详细为 ：{}", JSON.toJSONString(model));
            //TODO 将导入的数据做保存数据库操作
            }
            log.info("从Excel导入数据一共 {} 行 ", list.size());
            } catch (IOException e) {
            log.error("导入失败：{}", e.getMessage());
            } catch (Exception e1) {
            log.error("导入失败：{}", e1.getMessage());
            }
            return "导入成功";
            }


                }
